var uid = getParameter("uid");
var code=getParameter("code");
var now = new Date();
$(function() {
	if(uid){
	charts(now);
	}else{
		getToken(code);
	}
});
function getToken(code_){
	var urlStr = serverPath + "/server/doctor/diabetes/login.do";
	var jsonStr = 
	    '{'+
	    '	   "requestid":"",'+
	    '	   "requestsource":"diabetes",'+
	    '	   "sessionid":"",'+
	    '	   "uid":"",'+
	    '	   "method":"taipinglogin",'+
	    '	   "version":"1.0",'+
	    '	   "requestlist":'+
	    '	   ['+
	    '	      {'+
	    '	         "code":"'+code_+'"'+
	    '	      }'+
	    '	   ]'+
	    '	}';
	
	UseAjax(urlStr, jsonStr, document.cookie, getUid, '1');

};

function getUid(data) {
	if(data.resultcode=='0'){
		uid = data.uid;
		charts(now);
	}else{
		alert(data.resultmsg);	
	}
}
function charts(now) {
	var urlStr = serverPath + "/app/doctor/diabetes/weightView.do";
	/*var jsonStr = '{method:"showWeightChart",UId:"' + UId + '",befdate:"'
			+ getLastDay(now, 3).format("yyyy-MM-dd") + '",aftdate:"'
			+ getNextDay(now, 3).format("yyyy-MM-dd") + '",day:"7"}';*/
	
	var jsonStr = 
	    '{'+
	    '	   "requestid":"",'+
	    '	   "requestsource":"diabetes",'+
	    '	   "sessionid":"",'+
	    '	   "uid":"'+uid+'",'+
	    '	   "method":"showWeightChart",'+
	    '	   "version":"1.0",'+
	    '	   "requestlist":'+
	    '	   ['+
	    '	      {'+
	    '	         "uid":"'+uid+'",'+
	    '            "befdate":"' + getLastDay(now, 3).format("yyyy-MM-dd") +'",'+
	    '            "aftdate":"' + getNextDay(now, 4).format("yyyy-MM-dd") +'",'+
	    '            "day":"7"'+
	    '	      }'+
	    '	   ]'+
	    '	}';
	
	
	UseAjax(urlStr, jsonStr, document.cookie, rt, '1');
}
function rt(data) {
	var datas = new Array();
	var wi = data.resultlist[0].records;
	if (wi.length > 0) {
		for (var int = 0; int < wi.length; int++) {
			datas[int] = [
					Date.UTC(wi[int].dates.substring(0, 4), wi[int].dates
							.substring(5, 7) - 1,
							wi[int].dates.substring(8, 10), wi[int].dates
									.substring(11, 13)), Number(wi[int].value) ];

		}
	}
	$('#container').highcharts(
			{

				title : {
					text : ''
				},
				subtitle : {
					text : ''//getLastDay(now, 3).format("yyyy-MM-dd")+" 到 "+getNextDay(now, 3).format("yyyy-MM-dd")
				},
				xAxis : {
					type : 'datetime',
					dateTimeLabelFormats : {
					//	second : '%H:%M:%S',
						minute : '%H:%M',
						hour : '%H:%M',
						day : '%m-%d',
						week : '%m-%d',
						month : '%m',
						year : '%Y'
					},
					min : Date.UTC(now.getFullYear(), now.getMonth(), now
							.getDate() - 3),
					max : Date.UTC(now.getFullYear(), now.getMonth(), now
							.getDate() + 4),
					maxPadding : 5,
					gridLineWidth : 1,
					gridLineDashStyle : 'Dash',// 样式 虚线
					tickPixelInterval : 50
				},
				yAxis : {
					min : 20, // 固定列最小值
					max : 150,
					title : {
						//align: 'high',
		                //offset: 10,
		                text: 'KG'
		                //rotation: 0,
		                //y: 20
					},
					labels : {
						formatter : function() {
							return "" + this.value ;
						}
					} 
					
				},
				tooltip : {
					formatter : function() {
					}
				},
				plotOptions : {
					line : {
						dataLabels : {
							enabled : true
						},
						marker: {
		                    enabled: true
		                },
						enableMouseTracking : false
					}
				},
				credits : {
					enabled : false
				},
				legend : {
					enabled : false
				},
				series : [ {
					pointPadding : 0,// 数据点之间的距离值
					groupPadding : 0,// 分组之间的距离值
					name : null,
					data : datas
				} ]
			});
}

$(function() {
	// swipe 的dome
	$("#container").swipe(
			{
				swipeLeft : function(event, direction, distance, duration,
						fingerCount) {
					now = getNextDay(now, 7);
					charts(now);
				},
				swipeRight : function(event, direction, distance, duration,
						fingerCount) {
					now = getLastDay(now, 7);
					charts(now);
				}
			});

});
